/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CCPVSimulationPackage;

import GISPackage.Region;
import GISPackage.Regions;

import GUIUtilities.CoalitionUtilities;
import GUIUtilities.ListUtilities;
import SocialMatrixPackage.SocialMatrix;
import ccpv.FileManager;
import ccpv.Person;
import ccpv.RunTimeSettings;
import ccpv.Utilities;
import java.io.File;
import java.io.IOException;
import java.util.List;
import multiarray.MultiArray;
import multiarray.RecursiveAccess;
import solver.Results;

/**
 *
 * @author Heather
 */
public class SimTest
{

  private static boolean checkPop(MultiArray<Person> pop)
  {
    List<int[]> allCombos = RecursiveAccess.generateAllPermutations(pop.getDimensions());
    System.out.println(ListUtilities.getIntArrayString(pop.getDimensions()));
    System.out.println("number of objects: " + pop.numObjects);

    for (int[] i : allCombos)
    {
      if (pop.getElementAtIndex(i) == null)
      {
        System.out.println("null found");
        return false;
      }
    }

    return true;
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args)
  {
    // TODO code application logic here
    Regions regions = new Regions();

    try
    {
      regions.loadStuctureVariables(new File("c:/CCPV Data/all structure variables.csv"));
    } catch (IOException e)
    {
      System.out.println("bah humbug");
    }

    Region Malaysia = null;
    Region Indonesia = null;
    for (Region r : regions.regions)
    {
      if (r.name.equals("Malaysia"))
      {
        System.out.println("FOUND MALAYSIA");
        Malaysia = r;
      }
      if (r.name.equals("Indonesia"))
      {
        Indonesia = r;
      }
    }
    if (Indonesia == null)
    {
      System.out.println("NULL!");
    }
    else
    {

      System.out.println(Indonesia.toString());
      //CCPVSimulationIteration sim = new CCPVSimulationIteration("c:/CCPV Data/tests/format.txt", Malaysia);
      CCPVSimulationIteration sim = new CCPVSimulationIteration("c:/CCPV Data/tests/indoformat.txt", Indonesia);
      MultiArray<Float> preConflictPowers = sim.getSimPop().getPowerFromPersonsAsFloats();
      List<int[]> allCombos = RecursiveAccess.generateAllPermutations(sim.getSM().getDimensions());
      /*
      boolean found = sim.calculatePartitions();
      if (!found)
      {
        System.out.println("NO PARTITIONS FOUND");
      }
      else
      {
        System.out.println("PARTITIONS FOUND");
      }
       */
      List<List<List<int[]>>> partitions = sim.getAllPartitions();
      System.out.println("size of partitions: " + partitions.size());
      System.out.println("type of partitions: " + sim.getPartitionType());
      for (List<List<int[]>> partition : partitions)
      {
        System.out.println("size of this partition: " + partition.size());
      }
      List<List<int[]>> chosenPartition = partitions.get(0);
      Results r = sim.runConflict(chosenPartition);
      System.out.println("partition$$$$: ");
      System.out.println(CoalitionUtilities.getPartitionString(chosenPartition));
      float[][] thetas = r.getThetasF();
      System.out.println("number of coalitions: " + chosenPartition.size());
      System.out.println("dim 1 of thetas: " + thetas.length);
      System.out.println("dim 2 of thetas: " + thetas[0].length);
      System.out.println("is there a conflict: " + r.isConflict);
      for (int i = 0; i < chosenPartition.size(); i++)
      {
        for (int j = 0; j < chosenPartition.size(); j++)
        {
          System.out.println("thetas@@@ " + thetas[i][j]);
        }
      }

      //MultiArray<Float> preConflictPowers = sim.getSimPop().getPreConflictPowersAsFloats();
      MultiArray<Float> postConflictPowers = sim.getSimPop().getPowerFromPersonsAsFloats();
      for (int[] combo : allCombos)
      {
        float preConflictPower = preConflictPowers.getElementAtIndex(combo);
        float postConflictPower = postConflictPowers.getElementAtIndex(combo);
        System.out.println("pre conflict: " + preConflictPowers.getElementAtIndex(combo));
        System.out.println("post conflict: " + postConflictPowers.getElementAtIndex(combo));
        System.out.println(ListUtilities.getIntArrayString(combo) + " power diff: " + (postConflictPower - preConflictPower));
      }


      sim.runCoherence();

      /*
      RunTimeSettings rt = FileManager.csvReader("c:/CCPV Data/tests/indoformat.txt");
      //SocialMatrix sm = new SocialMatrix(CoalitionUtilities.getAscriptiveTree(rt.ascriptive_names));
      SocialMatrix sm = sim.getSM();
      MultiArray<Person> pop = sim.getSimPop().population;//Utilities.makePeople(rt);
      //MultiArray<Person> pop = Utilities.makePeople(rt);
      //List<int[]> allCombos = RecursiveAccess.generateAllPermutations(pop.getDimensions());
      for (int[] source : allCombos)
      {
        Person p = pop.getElementAtIndex(source);
        for (int[] target : allCombos)
        {
          double altruism = p.getAltruism(target);
          System.out.println(ListUtilities.getStringListString(sm.convertIndexIntoStringVector(source)) + ", " +
                  ListUtilities.getStringListString(sm.convertIndexIntoStringVector(target)) + ": " + altruism);
        }
      }*/


      /*
      RunTimeSettings rt = FileManager.csvReader("c:/CCPV Data/tests/indoformat.txt");
      MultiArray<Person> pop = Utilities.makePeople(rt);
      List<int[]> allCombos = RecursiveAccess.generateAllPermutations(pop.getDimensions());
      for (int[] combo : allCombos)
      {
      Person p = pop.getElementAtIndex(combo);
      System.out.println(ListUtilities.getIntArrayString(combo));
      System.out.println("p count: " + p.getQty());
      System.out.println("p power: " + p.getPower());
      System.out.println("");
      }*/

      //CCPVSimulationIteration si = sim.getNextIteration();
      //List<List<List<int[]>>> partitions = si.getPartitionsAsCrossSection();
      System.out.println("");
      System.out.println("");
      System.out.println("");
    //System.out.println(CoalitionUtilities.getPartitionsString(partitions));
    }

  }
}
